﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Novel.Entity;

namespace Novel.DAL
{
    public  partial class QueryBase
    {
        

      

        /// <summary>
        /// 利用分页存储过程
        /// </summary>
        public DataTable GetTable(PageEntity entity)
        {
            SQLHelper da = new SQLHelper();
            try
            {

                string sql = @"sp_GetPage";
                SqlCommand command = new SqlCommand(sql);
                command.CommandTimeout = 0;
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@tbName", SqlDbType.VarChar, 1000);
                command.Parameters["@tbName"].Value = entity.tbName;

                command.Parameters.Add("@colName", SqlDbType.VarChar, 100);
                command.Parameters["@colName"].Value = entity.PKey;

                command.Parameters.Add("@coltype", SqlDbType.Int, 4);
                command.Parameters["@coltype"].Value = (int)entity.PKeyType;

                command.Parameters.Add("@orderby", SqlDbType.VarChar, 100);
                command.Parameters["@orderby"].Value = entity.Orderby;

                command.Parameters.Add("@collist", SqlDbType.VarChar, 800);
                command.Parameters["@collist"].Value = entity.Collist;

                command.Parameters.Add("@pagesize", SqlDbType.Int, 4);
                command.Parameters["@pagesize"].Value = entity.PageSize;

                command.Parameters.Add("@page", SqlDbType.Int, 4);
                command.Parameters["@page"].Value = entity.CurrentPageIndex;

                command.Parameters.Add("@condition", SqlDbType.VarChar, 2000);
                command.Parameters["@condition"].Value = entity.Where;

                command.Parameters.Add("@pages", SqlDbType.Int);
                command.Parameters.Add("@rsCount", SqlDbType.Int);
                command.Parameters.Add("@curCount", SqlDbType.Int);

                command.Parameters["@pages"].Direction = ParameterDirection.Output;
                command.Parameters["@rsCount"].Direction = ParameterDirection.Output;
                command.Parameters["@curCount"].Direction = ParameterDirection.Output;

                DataSet ds = new DataSet();
                da.FillDataSet(command, ds);
                entity.TotalPages = (int)command.Parameters["@pages"].Value;
                entity.TotalPages = (int)command.Parameters["@rsCount"].Value;
                entity.CurCount = (int)command.Parameters["@curCount"].Value;

                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

      


        

    }
}
